<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--<el-form-item label="所属机构" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入所属机构"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="证件号码" prop="idNbr">
        <el-input
          v-model="queryParams.idNbr"
          placeholder="请输入证件号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phoneNbr">
        <el-input
          v-model="queryParams.phoneNbr"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="建卡日期">
        <el-date-picker
          v-model="daterangeCreateCardDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="预产期 ">
        <el-date-picker
          v-model="daterangeExpectedChildbirthDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="跟踪状态" prop="trackStatus">
        <el-select v-model="queryParams.trackStatus" placeholder="请选择跟踪状态" clearable>
          <el-option
            v-for="dict in dict.type.track_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="来源渠道" prop="customerChannel">
        <el-select v-model="queryParams.customerChannel" placeholder="请选择客户来源渠道" clearable>
          <el-option
            v-for="dict in dict.type.svc_qd_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="OPENID" prop="openid">
        <el-input
          v-model="queryParams.openid"
          placeholder="请输入OPENID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeImportDate"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:customerinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:customerinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:customerinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:customerinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="customerinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" width="50" align="center" prop="ckCustomerInfoId" />
      <el-table-column label="OPENID" width="140" align="center" prop="openid" />
      <!--<el-table-column label="所属机构" align="center" prop="deptId" />-->
      <el-table-column label="姓名" width="80" align="center" prop="customerName" />
      <el-table-column label="证件类型" width="80" align="center" prop="idType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.id_type" :value="scope.row.idType"/>
        </template>
      </el-table-column>
      <el-table-column label="证件号码" width="160"  align="center" prop="idNbr" />
      <el-table-column label="手机号码"  width="120" align="center" prop="phoneNbr" />
      <el-table-column label="所属渠道"  width="120" align="center" prop="customerChannel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.svc_qd_name" :value="scope.row.customerChannel"/>
        </template>
      </el-table-column>

      <el-table-column label="跟踪状态" width="120" align="center" prop="trackStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.track_status" :value="scope.row.trackStatus"/>
        </template>
      </el-table-column>

      <el-table-column label="家庭住址"  width="200" align="center" prop="address" />
      <el-table-column label="建卡日期" width="100" align="center" prop="createCardDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createCardDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="怀孕周数" width="100" align="center" prop="gestationalAge" />
      <el-table-column label="预产期 " width="100" align="center" prop="expectedChildbirthDate" >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectedChildbirthDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>



      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="创建时间" align="center" prop="importDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.importDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="120" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:customerinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:customerinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改客户信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!--<el-form-item label="所属机构" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入所属机构" />
        </el-form-item>-->

        <el-form-item label="客户来源渠道" prop="customerChannel">
          <el-select v-model="form.customerChannel" placeholder="请选择客户来源渠道">
            <el-option
              v-for="dict in dict.type.svc_qd_name"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="跟踪状态" prop="trackStatus">
          <el-select v-model="form.trackStatus" placeholder="请选择跟踪状态">
            <el-option
              v-for="dict in dict.type.track_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="openD" prop="openid">
          <el-input v-model="form.openid" placeholder="请输入openid" />
        </el-form-item>

        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="form.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="证件类型" prop="idType">
          <el-select v-model="form.idType" placeholder="请选择证件类型">
            <el-option
              v-for="dict in dict.type.id_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="证件号码" prop="idNbr">
          <el-input v-model="form.idNbr" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="手机号码" prop="phoneNbr">
          <el-input v-model="form.phoneNbr" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="家庭住址" prop="address">
          <el-input v-model="form.address" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="建卡日期" prop="createCardDate">
          <el-date-picker clearable
                          v-model="form.createCardDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择建卡日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="怀孕周数" prop="gestationalAge">
          <el-input v-model="form.gestationalAge" placeholder="请输入怀孕周数" />
        </el-form-item>
        <el-form-item label="预产期 " prop="expectedChildbirthDate">
          <el-date-picker clearable
                          v-model="form.expectedChildbirthDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择预产期 ">
          </el-date-picker>
        </el-form-item>



        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <!--<el-form-item label="创建时间" prop="importDate">
          <el-date-picker clearable
                          v-model="form.importDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listCustomerinfo, getCustomerinfo, delCustomerinfo, addCustomerinfo, updateCustomerinfo } from "@/api/system/customerinfo";

  export default {
    name: "Customerinfo",
    dicts: ['track_status', 'svc_qd_name', 'id_type'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户信息表格数据
        customerinfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 创建时间时间范围
        daterangeCreateCardDate: [],
        // 创建时间时间范围
        daterangeExpectedChildbirthDate: [],
        // 创建时间时间范围
        daterangeImportDate: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          deptId: null,
          customerName: null,
          idType: null,
          idNbr: null,
          phoneNbr: null,
          address: null,
          createCardDate: null,
          gestationalAge: null,
          expectedChildbirthDate: null,
          trackStatus: null,
          customerChannel: null,
          openid: null,
          notes: null,
          importDate: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {

          customerName: [
            { required: true, message: "客户名称不能为空", trigger: "blur" }
          ],
          idType: [
            { required: true, message: "证件类型不能为空", trigger: "change" }
          ],
          idNbr: [
            { required: true, message: "证件号码不能为空", trigger: "blur" }
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询客户信息列表 */
      getList() {
        this.loading = true;
        this.queryParams.params = {};
        if (null != this.daterangeCreateCardDate && '' != this.daterangeCreateCardDate) {
          this.queryParams.params["beginCreateCardDate"] = this.daterangeCreateCardDate[0];
          this.queryParams.params["endCreateCardDate"] = this.daterangeCreateCardDate[1];
        }
        if (null != this.daterangeExpectedChildbirthDate && '' != this.daterangeExpectedChildbirthDate) {
          this.queryParams.params["beginExpectedChildbirthDate"] = this.daterangeExpectedChildbirthDate[0];
          this.queryParams.params["endExpectedChildbirthDate"] = this.daterangeExpectedChildbirthDate[1];
        }
        if (null != this.daterangeImportDate && '' != this.daterangeImportDate) {
          this.queryParams.params["beginImportDate"] = this.daterangeImportDate[0];
          this.queryParams.params["endImportDate"] = this.daterangeImportDate[1];
        }
        listCustomerinfo(this.queryParams).then(response => {
          this.customerinfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          ckCustomerInfoId: null,
          deptId: null,
          customerName: null,
          idType: null,
          idNbr: null,
          phoneNbr: null,
          address: null,
          createCardDate: null,
          gestationalAge: null,
          expectedChildbirthDate: null,
          trackStatus: null,
          customerChannel: null,
          openid: null,
          notes: null,
          importDate: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.daterangeCreateCardDate = [];
        this.daterangeExpectedChildbirthDate = [];
        this.daterangeImportDate = [];
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.ckCustomerInfoId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加客户信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const ckCustomerInfoId = row.ckCustomerInfoId || this.ids
        getCustomerinfo(ckCustomerInfoId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改客户信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.ckCustomerInfoId != null) {
              updateCustomerinfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCustomerinfo(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ckCustomerInfoIds = row.ckCustomerInfoId || this.ids;
        this.$modal.confirm('是否确认删除客户信息编号为"' + ckCustomerInfoIds + '"的数据项？').then(function() {
          return delCustomerinfo(ckCustomerInfoIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/customerinfo/export', {
          ...this.queryParams
        }, `customerinfo_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
